//
//  FeedBackTableViewCell.m
//  SoupApp
//
//  Created by pro1 on 2018/11/20.
//  Copyright © 2018 pro2. All rights reserved.
//

#import "FeedBackTableViewCell.h"

@interface FeedBackTableViewCell ()

@property (strong,nonatomic) UIImageView *header;
@property (strong,nonatomic) UIImageView *contentBG;
@property (strong,nonatomic) QMUILabel *contentLabel;

@end

@implementation FeedBackTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.header];
        [self.contentView addSubview:self.contentBG];
        self.backgroundColor = RGB(245, 245, 245);
    }
    return self;
}

- (UIImageView *)header{
    if (!_header) {
        _header = [[UIImageView alloc] init];
        _header.clipsToBounds = YES;
        [_header setCorner:22.0];
        _header.contentMode = UIViewContentModeScaleAspectFill;
        _header.backgroundColor = [UIColor grayColor];
    }
    return _header;
}

- (QMUILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[QMUILabel alloc] init];
        _contentLabel.contentEdgeInsets = UIEdgeInsetsMake(13, 13, 13, 13);
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
        _contentLabel.textColor = [UIColor blackColor];
        _contentLabel.backgroundColor = [UIColor clearColor];
//        _contentLabel.layer.shadowColor = UIColorMakeWithHex(@"#999999").CGColor;
//        _contentLabel.layer.shadowOffset = CGSizeMake(0, 1);
//        _contentLabel.layer.shadowOpacity = 0.2;
//        _contentLabel.layer.shadowRadius = 10.0;
        _contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
//        _contentLabel.layer.cornerRadius = 10.0;
//        _contentLabel.clipsToBounds = YES;
//        [_contentLabel setCorner:10.0];
    }
    return _contentLabel;
}

- (UIImageView *)contentBG{
    if (!_contentBG) {
        _contentBG = [[UIImageView alloc] init];
        _contentBG.backgroundColor = UIColorMake(250, 250, 250);
        _contentBG.layer.shadowRadius = 10.0;
//        _contentBG.contentMode = UIViewContentModeScaleAspectFill;
        _contentBG.backgroundColor = [UIColor clearColor];
        [_contentBG addSubview:self.contentLabel];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.contentBG).offset(0);
            make.left.equalTo(self.contentBG).offset(0);
            make.right.equalTo(self.contentBG).offset(0);
        }];
    }
    return _contentBG;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
}

- (void)setModel:(FeedBackModel *)model{
    _model = model;
    if (_model) {
        [self layoutUI];
        if (_model.is_user == 2) {
            [self.header setImage:[UIImage imageNamed:@"用户默认头像"]];
        }else{
            [self.header sd_setImageWithURL:[NSURL URLWithString:[UserDefaults getUserImage]] placeholderImage:[UIImage imageNamed:@"用户默认头像"]];
        }
        self.contentLabel.text = _model.content;
    }
}

- (void)layoutUI{
    if (_model.is_user == 2) {
        [_header mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(13);
            make.left.equalTo(self.contentView).offset(20);
            make.width.height.offset(44);
        }];
        if (_model.contentSize.width < SCREEN_WIDTH - 121 && _model.contentSize.height < 25) {
            [_contentBG mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.header.mas_bottom).offset(10);
                make.left.equalTo(self.contentView).offset(20);
                make.bottom.equalTo(self.contentView).offset(-13);
                make.width.offset(self.model.contentSize.width + 26);
            }];
        }
        else{
            [_contentBG mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.header.mas_bottom).offset(10);
                make.left.equalTo(self.contentView).offset(20);
                make.right.equalTo(self.contentView).offset(-60);
                make.bottom.equalTo(self.contentView).offset(-13);
            }];
        }
//        [_contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.contentBG).offset(15);
//            make.right.equalTo(self.contentBG).offset(0);
//        }];
        UIImage *image = [UIImage imageNamed:@"img_chat_right"];
        [_contentBG setImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2) resizingMode:UIImageResizingModeStretch]];
    }
    else{
        [_header mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(13);
            make.right.equalTo(self.contentView).offset(-20);
            make.width.height.offset(44);
        }];
        
        if (_model.contentSize.width < SCREEN_WIDTH - 121 && _model.contentSize.height < 25) {
            [_contentBG mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.header.mas_bottom).offset(10);
                make.right.equalTo(self.contentView).offset(-20);
                make.bottom.equalTo(self.contentView).offset(-13);
                make.width.offset(self.model.contentSize.width + 26);
            }];
        }
        else{
            [_contentBG mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.header.mas_bottom).offset(10);
                make.right.equalTo(self.contentView).offset(-20);
                make.left.equalTo(self.contentView).offset(60);
                make.bottom.equalTo(self.contentView).offset(-13);
            }];
        }
//        [_contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.contentBG).offset(0);
//            make.right.equalTo(self.contentBG).offset(-15);
//        }];
        UIImage *image = [UIImage imageNamed:@"img_chat_left"];
        [_contentBG setImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2) resizingMode:UIImageResizingModeStretch]];
    }
    if (_model.contentSize.height > 25) {
        _contentLabel.textAlignment = NSTextAlignmentLeft;
    }
    else{
        _contentLabel.textAlignment = NSTextAlignmentCenter;
    }
}

@end
